﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BackTask.DashBoard.Lisenters;
using BackTask.DashBoard.Models;
using Microsoft.AspNetCore.Mvc;
using Quartz;
using Quartz.Impl.Calendar;
using Quartz.Impl.Triggers;

namespace BackTask.DashBoard.Api
{
    [Route("job")]
    [ApiController]
    public class JobController : ControllerBase
    {
        [HttpGet("jobs")]
        public async Task<IActionResult> GetJobs()
        {
            var jobs = await JobManager.CreateInstance().GetAllJob();

            return this.Ok(jobs);
        }

        [HttpGet("pauseJob")]
        public IActionResult PauseJob([FromQuery] string name, [FromQuery] string groupName)
        {
            JobManager.CreateInstance().PauseJob(name, groupName);

            return this.Ok("");
        }

        [HttpGet("startJob")]
        public IActionResult StartJob([FromQuery] string name, [FromQuery] string groupName)
        {
            JobManager.CreateInstance().StartJob(name, groupName);

            return this.Ok("");
        }

        [HttpGet("rescheduleJob")]
        public IActionResult RescheduleJob([FromQuery] string name, [FromQuery] string groupName)
        {
            JobManager.CreateInstance().RescheduleJob(name, groupName);

            return this.Ok("");
        }

        [HttpGet("deleteJob")]
        public IActionResult DeleteJob([FromQuery] string name, [FromQuery] string groupName)
        {
            JobManager.CreateInstance().DeleteJob(name, groupName);

            return this.Ok("");
        }

        [HttpPost("new")]
        public async Task<IActionResult> CreateJob([FromBody] CreateJobRequest createJobRequest)
        {
            var job = await JobManager.CreateInstance().CreateJob(createJobRequest);

            return this.Ok(job);
        }

        [HttpPost("edit")]
        public IActionResult EditJob([FromBody] CreateJobRequest createJobRequest)
        {
            var job = JobManager.CreateInstance().EditJob(createJobRequest);
            return this.Ok(job);
        }

        [HttpGet("validate")]
        public IActionResult ValidateCron([FromQuery] string cronExpression)
        {
            if (!string.IsNullOrEmpty(cronExpression))
            {
                bool valid = CronExpression.IsValidExpression(cronExpression);
                int top = 10;
                List<string> executeTimeArray = new List<string>();
                if (valid)
                {
                    CronTriggerImpl cron = new CronTriggerImpl("testName", "testGroup", cronExpression);
                    ICalendar calendar = new BaseCalendar(TimeZoneInfo.Local);
                    var list = TriggerUtils.ComputeFireTimes(cron, calendar, top);
                    if (list != null)
                    {
                        foreach (var item in list)
                        {
                            executeTimeArray.Add(item.ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }
                }

                return this.Ok(new { validStatus = valid, ExecuteTimeArray = executeTimeArray });
            }
            else
            {
                return this.Ok(new { validStatus = false });
            }
        }
    }
}
